<?php

namespace app\model\pms;

use plugin\agent\app\model\AgentGoods;
use think\db\Query;
use think\Model;

/**
 * t_goods 商品表
 * @property integer $id 商品id(主键)
 * @property integer $type 商品类型 1:实物 2:虚拟
 * @property string $name 商品名称
 * @property string $sub_name 商品副标题
 * @property integer $cate_id1 商品分类1级
 * @property integer $cate_id2 商品分类2级
 * @property integer $cate_id3 商品分类3级
 * @property string $unit 单位
 * @property string $slider_image 轮播图
 * @property string $video_src 视频地址
 * @property integer $sales 销量
 * @property integer $views 浏览量
 * @property integer $collects 收藏数
 * @property integer $spec 规格 1:单规格 2:多规格
 * @property string $price 销售价格
 * @property string $cost_price 成本价格
 * @property string $original_price 划线原价
 * @property string $postage 邮费
 * @property string $spu 商品编码
 * @property integer $stock 库存数
 * @property string $weight 重量 KG
 * @property string $volume 体积 m³
 * @property string $user_label 关联的用户标签
 * @property integer $is_show 是否上架 1:是 2:否
 * @property integer $is_hot 是否热门 1:是 2:否
 * @property integer $is_recommend 是否推荐 1:是 2:否
 * @property integer $is_new 是否新品 1:是 2:否
 * @property integer $is_del 是否删除 1:是 2:否
 * @property integer $shipping_tpl_id 运费模板id
 * @property integer $freight 运费设置 1:固定 2:运费模板
 * @property string $seo_title seo标题
 * @property string $seo_keywords seo关键词
 * @property string $seo_desc seo描述
 * @property string $create_time 创建时间
 * @property string $update_time 更新时间
 */
class Goods extends Model
{
    /**
     * The connection name for the model.
     *
     * @var string|null
     */
    protected $connection = 'mysql';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 't_goods';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $pk = 'id';

    const FREIGHT_TYPE_FREE = 1; //免邮
    const FREIGHT_TYPE_TEMPLATE = 2; //运费模板
    const FREIGHT_TYPE_FIXED = 3; //固定

    protected $append = [
        "cate_ids"
    ];

    public function setSliderImageAttr($value)
    {
        $value = json_encode($value, JSON_UNESCAPED_UNICODE);

        return $value;
    }

    public function getSliderImageAttr($value)
    {
        $value = json_decode($value ?? "{}", true) ?? [];
        foreach ($value as &$v) {
            $v = img_url($v);
        }
        return $value;
    }

    public function setAttrAttr($value)
    {
        $value = json_encode($value ?? [], JSON_UNESCAPED_UNICODE);
        return $value;
    }

    public function getAttrAttr($value)
    {
        $value = json_decode($value ?? "[]", true) ?? [];
        return $value;
    }

    public function setVideoSrcAttr($value)
    {
        $value = json_encode($value, JSON_UNESCAPED_UNICODE);

        return $value;
    }

    public function getVideoSrcAttr($value)
    {
        $value = json_decode($value ?? "{}", true) ?? [];
        foreach ($value as &$v) {
            $v = img_url($v);
        }
        return $value;
    }

    public function getTagIdsAttr($value)
    {
        return array_map("intval", $value ? explode(",", $value) : []);
    }

    public function setTagIdsAttr($value)
    {
        return implode(",", $value);
    }


    public function cate1()
    {
        return $this->hasOne(GoodsCate::class, 'id', 'cate_id1');
    }

    public function cate2()
    {
        return $this->hasOne(GoodsCate::class, 'id', 'cate_id2');
    }

    public function cate3()
    {
        return $this->hasOne(GoodsCate::class, 'id', 'cate_id3');
    }

    public function getCateIdsAttr($value, $data)
    {
        return array_filter([$data['cate_id1'] ?? 0, $data['cate_id2'] ?? 0, $data['cate_id3'] ?? 0]);
    }

    public function brand()
    {
        return $this->hasOne(GoodsBrand::class, 'id', 'brand_id');
    }

    public function goodsspec()
    {
        return $this->hasOne(GoodsSpec::class, 'goods_id', 'id');
    }

    public function goodssku()
    {
        return $this->hasMany(GoodsSku::class, 'goods_id', 'id');
    }

    static function getCateWith()
    {
        return [
            "cate1" => function (Query $query) {
                $query->field(['id', 'name']);
            },
            "cate2" => function (Query $query) {
                $query->field(['id', 'name']);
            },
            "cate3" => function (Query $query) {
                $query->field(['id', 'name']);
            },
            "brand" => function (Query $query) {
                $query->field(['id', 'name']);
            },
        ];
    }

    public function agentgoods()
    {
        return $this->hasOne(AgentGoods::class, 'goods_id', 'id');
    }

}
